<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/10/22
 * Time: 13:25
 */

namespace app\common\service;


class ServerUser
{

    function findAll($field = '*')
    {
        return db('server_user')->field($field)->select();
    }
    /**
     * @name 查询
     * @param $condition
     * @return array|null|\PDOStatement|string|\think\Model
     * @author wx
     * @date 2019/10/21 16:45
     */
    function findData($condition)
    {
        return db('server_user')->where($condition)->find();
    }

    /**
     * @name 修改
     * @param $condition
     * @param $updateData
     * @return int|string
     * @author wx
     * @date 2019/10/22 14:18
     */
    function modify($condition, $updateData)
    {
        return db('server_user')->where($condition)->update($updateData);
    }

    /**
     * @name 列表
     * @param $params
     * @param $limit
     * @return array
     * @author wx
     * @date 2019/10/28 15:51
     */
    function list($params, $limit)
    {
        $db = db('server_user')->where('level','<>',0);
        //按条件查询
        if (isset($params['real_name']) && !empty($params['real_name'])) {
            $db->where('real_name','like','%'.$params['real_name'].'%');
        }
        if (isset($params['show_name']) && !empty($params['show_name'])) {
            $db->where('show_name','like','%'.$params['show_name'].'%');
        }
        if (isset($params['ctime']) && !empty($params['ctime'])) {
            $ctime_start = strtotime($params['ctime']);
            $db->where('ctime', 'between', [$ctime_start, $ctime_start + 86400]);
        }

        if (isset($params['last_login_time']) && !empty($params['last_login_time'])) {
            $last_login_start = strtotime($params['last_login_time']);
            $db->where('last_login_time', 'between', [$last_login_start,$last_login_start + 86400]);
        }

        $field = 'id,show_name,real_name,ctime,last_login_time';

        return $db->order('id desc')->field($field)->paginate($limit)->toArray();
    }

    /**
     * @name 添加
     * @param $addData
     * @return int|string
     * @author wx
     * @date 2019/10/28 15:52
     */
    function add($addData)
    {
        return db('server_user')->insert($addData);
    }

    /**
     * @name 删除
     * @param $condition
     * @return int
     * @author wx
     * @date 2019/10/28 16:06
     */
    function del($condition)
    {
        return db('server_user')->where($condition)->delete();
    }

    /**
     * @name: 通过 id 获取用户详情
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-20 11:21:49
     */
    function getUserById ($id_a)
    {
        $return = db('ServerUser')->whereIn('id', $id_a)->column('id,show_name,real_name', 'id');

        return $return;
    }



}